package my.ezbart;

import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.NSIndexPath;
import org.xmlvm.iphone.UIAlertView;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UIImage;
import org.xmlvm.iphone.UILabel;
import org.xmlvm.iphone.UIScreen;
import org.xmlvm.iphone.UITableView;
import org.xmlvm.iphone.UITableViewCell;
import org.xmlvm.iphone.UITableViewController;
import org.xmlvm.iphone.UITableViewDataSource;
import org.xmlvm.iphone.UITableViewDelegate;
import org.xmlvm.iphone.UITableViewStyle;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewController;

/**
 * @author Cecil Holmes
 * @version 0.1
 * 
 * MoreInfo.java (4th tab on the tab bar)
 * 
 * View Controller that displays emergency and non-emergency phone numbers to be used by
 * BART travelers in many situations. When a phone number is tapped a phone call will
 * automatically be launched.
 * 
 * Holds a Table View with 2 tables that act as buttons to make emergency calls view. The call
 * is displayed as an alert view mimicking the normal behavior when phone numbers are tapped in iOS.
 */
public class MoreInfo extends UIViewController {
	private String[] emergency = {"Emergency -- 911", "BART Police -- (510) 464-7000", "Non-Emergency -- (877) 677-7000" };
	private String[] bartInfo = {"Elevator Info -- (888) 235-3828", "Lost & Found -- (510) 464-7090", "Trip Planning -- (415) 989-2278"};
	private UIView root = new UIView(UIScreen.mainScreen().getApplicationFrame());
	private UITableViewController tc = new UITableViewController(UITableViewStyle.Grouped);
	private UITableView info = tc.getTableView();
	
	public MoreInfo() {
		this.setTitle("More Info");
		this.getTabBarItem().setImage(UIImage.imageNamed("75-phone.png"));
		
	}
	
    private String getItem(int section, int row) {
        return section == 0 ? emergency[row] : bartInfo[row];
    }

    public class TableDataSource extends UITableViewDataSource {

        @Override
        public int numberOfSectionsInTableView(UITableView table) {

            return 2;
        }

        @Override
        public String titleForHeaderInSection(UITableView table, int section) {
            /*
             * Section 0 has the heading "Emergencies" and section 1 has the heading
             * "Bart Info".
             */
            return section == 0 ? "Emergencies" : "BART Info";
        }

		@Override
		public UITableViewCell cellForRowAtIndexPath(UITableView view, NSIndexPath index) {
            UITableViewCell tc = new UITableViewCell();
            UILabel detailLabel = tc.getTextLabel();
            
            detailLabel.setText(getItem(index.getSection(), index.getRow()));

            return tc;
		}

		@Override
		public int numberOfRowsInSection(UITableView view, int section) {
			//origin, destination, time
			return section == 0 ? emergency.length : bartInfo.length;
		}
    }

    public class TableDelegate extends UITableViewDelegate {
    	
    	public void didSelectRowAtIndexPath(UITableView tableview, NSIndexPath index) {
            
    		String number = getItem(index.getSection(), index.getRow());
    		
    		number = number.substring(number.indexOf("--")+3);

    		UIAlertView alert = new UIAlertView(null, number, null, "Call");
            alert.show();
            
            info.reloadData();
        }
    }

    @Override
    public void loadView() {
    	info.setBounces(false);
    	info.setScrollEnabled(false);
		info.setFrame(new CGRect(0,0, UIScreen.mainScreen().getApplicationFrame().size.width, info.getFrame().size.height));
    	info.setDataSource(new TableDataSource());
    	info.setDelegate(new TableDelegate());
    	
    	root.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
    	root.addSubview(info);
    	
    	this.setView(root);
    }
}